////
//
/// \file ./test/immediate_number_node_test.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/immediate_number_node.hpp>

#include <inttypes.h>
#include <string>

#include <gtest/gtest.h>

#include <inc/parser/utilities.hpp>

namespace inc {
namespace parser {

using std::string;

TEST(ImmediateNumberNodeTest, parse_basic_ints)
{
  struct test_struct {
    const string text;
    uint64_t number;
  } data[] = { { "0", 0 },
               { "1", 1 },
               { "8", 8 },
               { "10", 10 },
               { "1024", 1024 },
               { "3141592653589793", 3141592653589793ll },
               { "0x0000", 0 },
               { "0x0001", 1 },
               { "0xff", 0xff },
               { "0xaBaB01Dc", 0xabab01dc },
               { "0b0", 0 },
               { "0b00000001", 1 },
               { "0b101", 5 },
               { "0o0", 0 },
               { "00", 0 },
               { "0777", 0777 },
               { "0o10677", 010677 },
             };
  for( int i = 0 ; i < sizeof( data ) / sizeof( data[0] ) ; ++i )
  {
    ImmediateNumberNode node;
    string::const_iterator last_parsed;
    EXPECT_TRUE( node.parse( data[i].text.begin(),
                             data[i].text.end(),
                             last_parsed ) )
        << "Failed to parse: '" << data[i].text << "'";
    EXPECT_EQ( data[i].text, string( data[i].text.begin(), last_parsed ) );
    EXPECT_EQ( data[i].text, node.get_text() );
  }
}

TEST(ImmediateNumberNodeTest, parse_fails)
{
  const string data[] = { "x0", "foo", };
  for( int i = 0 ; i < elementsof( data ) ; ++i )
  {
    ImmediateNumberNode node;
    string::const_iterator last_parsed = data[i].end();
    EXPECT_FALSE( node.parse( data[i].begin(), data[i].end(), last_parsed ) )
        << "Erroneously parsed: '" << data[i] << "'";
    EXPECT_EQ( string(), string( data[i].begin(), last_parsed ) );
  }
}

} //  end parser namespace
} //  end inc namespace
